package com.example.util;

import com.alipay.api.kms.aliyun.exceptions.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 发送短信验证码工具类
 */
public class SMSUtil {
    public static void sendMessage(String regionId,String accessKeyId,String
            accessKeySecret,String signName,String templateCode,String phoneNumbers, String code) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId,
                accessKeyId, accessKeySecret);
                IAcsClient client = new DefaultAcsClient(profile);
                SendSmsRequest request = new SendSmsRequest();
                request.setSysRegionId(regionId);
                request.setPhoneNumbers(phoneNumbers);
                request.setSignName(signName);
                request.setTemplateCode(templateCode);
                request.setTemplateParam("{\"code\":\"" + code + "\"}");
                try {
                     SendSmsResponse response = client.getAcsResponse(request);
                          // 判读是否发送成功
                         if (response.getCode().equals("OK")) {
                             System.out.println("短信发送成功，返回结果：" + response.getMessage());
                         } else {
                             System.out.println("短信发送失败，原因：" + response.getMessage());
                          }
                 }catch (ServerException e) {
                    e.printStackTrace();
                    System.out.println("短信发送失败！");
                } catch (com.aliyuncs.exceptions.ClientException e) {
                    e.printStackTrace();
                }
    }
}
